<template>
   <a-spin :spinning="confirmLoading">
     <j-form-container :disabled="formDisabled">
       <!-- 主表单区域 -->
       <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
         <a-row>
       <!--    <a-col :xs="24" :sm="12">
            <a-form-model-item label="订单号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderNo">
              <a-input v-model="model.orderNo" placeholder="请输入订单号" disabled></a-input>
            </a-form-model-item>
          </a-col>-->
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="顾客" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buyerId">
              <j-dict-select-tag type="list" v-model="model.buyerId"  dictCode="fep_buyers,username,id" placeholder="请选择顾客" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="实付总价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="payTotalPrice">
              <a-input-number v-model="model.payTotalPrice" placeholder="请输入实付总价" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="下单时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderTime">
              <j-date placeholder="请选择下单时间" v-model="model.orderTime" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :xs="24" :sm="12">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remark">
              <a-input v-model="model.remark" placeholder="请输入备注" ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
     </j-form-container>
      <!-- 子表单区域 -->
      <a-tabs v-model="activeKey" @change="handleChangeTabs">
        <a-tab-pane tab="付款记录" :key="refKeys[0]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[0]"
            :loading="fepInstallmentsTable.loading"
            :columns="fepInstallmentsTable.columns"
            :dataSource="fepInstallmentsTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
        <a-tab-pane tab="订单详情" :key="refKeys[1]" :forceRender="true">
          <j-editable-table
            :ref="refKeys[1]"
            :loading="fepOrdersProductTable.loading"
            :columns="fepOrdersProductTable.columns"
            :dataSource="fepOrdersProductTable.dataSource"
            :maxHeight="300"
            :disabled="formDisabled"
            :rowNumber="true"
            :rowSelection="true"
            :actionButton="true"/>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
</template>

<script>

  import { FormTypes,getRefPromise,VALIDATE_NO_PASSED } from '@/utils/JEditableTableUtil'
  import { JEditableTableModelMixin } from '@/mixins/JEditableTableModelMixin'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'FepOrdersForm',
    mixins: [JEditableTableModelMixin],
    components: {
    },
    data() {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        model:{
        },
        validatorRules: {
         /*   orderNo: [
              { required: true, message: '请输订单号!'},
           ], */
           buyerId: [
              { required: true, message: '请选择顾客!'},
           ],
           payTotalPrice: [
              { required: true, message: '请输入付款总价'},
              { pattern: /^(([1-9][0-9]*)|([0]\.\d{0,2}|[1-9][0-9]*\.\d{0,2}))$/, message: '请输入正确的金额!'},
           ],
           orderTime: [
              { required: true, message: '请输入下单时间!'},
           ],
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        refKeys: ['fepInstallments', 'fepOrdersProduct', ],
        tableKeys:['fepInstallments', 'fepOrdersProduct', ],
        activeKey: 'fepInstallments',
        // 付款记录
        fepInstallmentsTable: {
          loading: false,
          dataSource: [],
          columns: [
   /*          {
              title: '订单号',
              key: 'orderNo',
              type: FormTypes.input,
              disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            }, 
 */
            {
              title: '支付金额',
              key: 'amount',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' },
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'}],
            },
            {
              title: '付款时间',
              key: 'payTime',
              type: FormTypes.date,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '付款方式',
              key: 'payType',
              type: FormTypes.select,
              dictCode:"fep_pay_type",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '备注',
              key: 'remark',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
            },
          ]
        },
        // 订单详情
        fepOrdersProductTable: {
          loading: false,
          dataSource: [],
          columns: [
         /*    {
              title: '订单号',
              key: 'orderNo',
              type: FormTypes.select,
              dictCode:"",
              disabled:true,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            }, */
            {
              title: '水果',
              key: 'productId',
              type: FormTypes.select,
              dictCode:"fep_products,name,id",
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '付款单价',
              key: 'unitPrice',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' },
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'}],
            },
            {
              title: '数量',
              key: 'quantity',
              type: FormTypes.input,
              width:"200px",
              placeholder: '请输入${title}',
              defaultValue:'',
              validateRules: [{ required: true, message: '${title}不能为空' },
              { pattern: /^-?\d+$/, message: '请输入整数!'}],
            },
          ]
        },
        url: {
          add: "/fep/fepOrders/add",
          edit: "/fep/fepOrders/edit",
          fepInstallments: {
            list: '/fep/fepOrders/queryFepInstallmentsByOrderNo'
          },
          fepOrdersProduct: {
            list: '/fep/fepOrders/queryFepOrdersProductByOrderNo'
          },
        }
      }
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
    },
    methods: {
     addBefore(){
            this.fepInstallmentsTable.dataSource=[]
            this.fepOrdersProductTable.dataSource=[]
      },
      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        this.$nextTick(() => {
        })
        // 加载子表数据
        if (this.model.orderNo) {
          let params = { id: this.model.orderNo }
          this.requestSubTableData(this.url.fepInstallments.list, params, this.fepInstallmentsTable)
          this.requestSubTableData(this.url.fepOrdersProduct.list, params, this.fepOrdersProductTable)
        }
      },
      //校验所有一对一子表表单
    validateSubForm(allValues){
        return new Promise((resolve,reject)=>{
          Promise.all([
          ]).then(() => {
            resolve(allValues)
          }).catch(e => {
            if (e.error === VALIDATE_NO_PASSED) {
              // 如果有未通过表单验证的子表，就自动跳转到它所在的tab
              this.activeKey = e.index == null ? this.activeKey : this.refKeys[e.index]
            } else {
              console.error(e)
            }
          })
        })
    },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          fepInstallmentsList: allValues.tablesValue[0].values,
          fepOrdersProductList: allValues.tablesValue[1].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     close() {
        this.visible = false
        this.$emit('close')
        this.$refs.form.clearValidate();
      },

    }
  }
</script>

<style scoped>
</style>